<?php
/**
* Exclude magic_quotes_gpc influence. Implemented on the application level
* as PHP can be started in CGI mode.
*
*/
if (get_magic_quotes_gpc()) {
    array_apply_recursive($_GET, 'stripslashes');
    array_apply_recursive($_POST, 'stripslashes');
    array_apply_recursive($_COOKIE, 'stripslashes');
    array_apply_recursive($_REQUEST, 'stripslashes');
}

class FinalView_Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{
    
    /**
    * Add view helpers path
    * 
    */
    protected function _initViewHelpers() 
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->addHelperPath('FinalView/View/Helper', 'FinalView_View_Helper');
    }
    
    /**
    * Init Doctrine
    * 
    */
    protected function _initDoctrine()
    {
        require_once 'Doctrine.php';
        
        $loader = Zend_Loader_Autoloader::getInstance();
        $loader->pushAutoloader(array('Doctrine', 'autoload'));
        
        $manager = Doctrine_Manager::getInstance();
        
        $manager->setAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS, true);
        $manager->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
        $manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, 
            Doctrine::MODEL_LOADING_CONSERVATIVE);
        
        // Add models and generated base classes to Doctrine autoloader
        $doctrineConfig = $this->getOption('doctrine');
        Doctrine::loadModels($doctrineConfig['models_path']);
        
        $manager->openConnection($doctrineConfig['connection_string']);
        Doctrine_Manager::connection()->setCharset('UTF8');
        
        return $manager;
    }
    
    /**
    * Add routes to the router
    * 
    * @param array $routes
    */
    protected function _addRoutes(array $routes) 
    {
        foreach ($routes as $entry) {
            $route = 'Zend_Controller_Router_Route_' 
                . ucfirst(strtolower($entry['type']));
            Zend_Controller_Front::getInstance()->getRouter()->addRoute(
                $entry['name'], 
                new $route
                (
                    $entry['url'], 
                    array
                    (
                        'module'        => $entry['module'], 
                        'controller'    => $entry['controller'], 
                        'action'        => $entry['action']
                    )
                )
            );
        }
    }
    
}